var marquee = (function () {

    function init(id) {
        initMq(getId(id));
        return getId(id);
    }

    function initMq(obj) {
        var objs;
        //定义跑马灯对象的自定义属性
        obj.currentItem = -1;
        obj.loopDelay = 15;
        obj.loopItems = new Array();
        obj.loopTimer = null;
        obj.speedX = 2;
        obj.speedY = 2;
        //定义跑马灯对象的自定义方法
        obj.loop = mq_loop;
        obj.start = mq_startLoop;
        obj.stop = mq_stopLoop;
        obj.isStart = false;
        //定义跑马灯对象的事件
        // obj.onmouseover = function(){ this.stop(); }
        // obj.onmouseout = function(){ this.loop(); }

        //获取跑马灯对象的所有子元素
        objs = obj.getElementsByTagName("div");
        for (var i = 0; i < objs.length; i++) {
            //在loopItems属性中记录子元素
            obj.loopItems.push(objs[i]);
            //自定义子元素的属性和方法
            objs[i].index = i;
            objs[i].move = move;
            objs[i].reset = mq_reset;
            //初始化子元素的状态
            objs[i].reset();
        }
    }

    function move(x) {
        this.style.left = x + "px";
    }

    function mq_loop() {
        var obj;
        var id = this.id;
        if (this.currentItem >= this.loopItems.length) this.currentItem = 0;
        obj = this.loopItems[this.currentItem];
        if (obj.offsetLeft >= -obj.offsetWidth) {
            //向左卷动
            obj.move(obj.offsetLeft - this.speedX);
        } else {
            //重置该子元素并停止
            obj.reset();
            this.stop();
        }
    }

    function mq_reset() {
        var p = this.parentNode;
        this.move(p.offsetWidth);
    }

    function mq_startLoop() {
        console.log("start");
        var that = this;
        this.isStart = true;
        $(this).show();
        for (var i = 0; i < this.loopItems.length; i++) this.loopItems[i].reset();
        this.currentItem = 0;
        this.loopTimer = setInterval(function () {
            that.loop();
        }, this.loopDelay);
    }

    function mq_stopLoop() {
        console.log("stop");
        this.isStart = false;
        $(this).hide();
        clearInterval(this.loopTimer);
    }

    function getId(str) {
        return (document.getElementById(str));
    }

    return {init: init};
})();